﻿using Sgr.Security.Permissions;
using Sgr.UPMS.Application.Commands.Roles;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Sgr.UPMS
{
    /// <summary>
    /// UPMS权限定义
    /// </summary>
    public class Permissions : IPermissionProvider
    {
        #region 组织机构管理权限

        public const string CreateOrg = "Sgr.UPMS.CreateOrg";
        public const string UpdateOrg = "Sgr.UPMS.UpdateOrg";
        public const string CancellationOrg = "Sgr.UPMS.CancellationOrg";
        public const string RecoverOrg = "Sgr.UPMS.RecoverOrg";
        public const string ViewOrg = "Sgr.UPMS.ViewOrg";
        public const string ViewCurrentOrg = "Sgr.UPMS.ViewCurrentOrg";
        //public const string AuthenticationOrg = "Sgr.UPMS.AuthenticationOrg";
        public const string AssociatedParentOrg = "Sgr.UPMS.AssociatedParentOrg";

        #endregion 组织机构管理权限

        #region 组织邀请管理权限

        public const string ManageOrgInvitation = "Sgr.UPMS.ManageOrgInvitation";
        public const string ViewOrgInvitation = "Sgr.UPMS.ViewOrgInvitation";

        #endregion 组织邀请管理权限

        #region 账户管理权限

        public const string CreateUser = "Sgr.UPMS.CreateUser";
        public const string UpdateUser = "Sgr.UPMS.UpdateUser";
        public const string DeleteUser = "Sgr.UPMS.DeleteUser";
        public const string ViewUser = "Sgr.UPMS.ViewUser";
        public const string ModifyUserStatus = "Sgr.UPMS.ModifyUserStatus";
        public const string ResetPassword = "Sgr.UPMS.ResetPassword";

        #endregion 账户管理权限

        #region 个人中心权限

        public const string ModifyUser = "Sgr.UPMS.ModifyUser";
        public const string ModifyPassword = "Sgr.UPMS.ModifyPassword";

        #endregion 个人中心权限

        #region 角色管理权限

        //public const string RoleCreate = AllocateFunctionPermissionCommandHandle.RoleCreate;
        //public const string RoleUpdate = AllocateFunctionPermissionCommandHandle.RoleUpdate;
        //public const string RoleDelete = AllocateFunctionPermissionCommandHandle.RoleDelete;
        //public const string RoleView = AllocateFunctionPermissionCommandHandle.RoleView;
        //public const string RoleModifyStatus = AllocateFunctionPermissionCommandHandle.RoleModifyStatus;
        //public const string RoleAllocateFunction = AllocateFunctionPermissionCommandHandle.RoleAllocateFunction;

        #endregion 角色管理权限

        #region 职务管理权限

        public const string CreateDuty = "Sgr.UPMS.CreateDuty";
        public const string UpdateDuty = "Sgr.UPMS.UpdateDuty";
        public const string DeleteDuty = "Sgr.UPMS.DeleteDuty";
        public const string ViewDuty = "Sgr.UPMS.ViewDuty";
        public const string ModifyDutyStatus = "Sgr.UPMS.ModifyDutyStatus";

        #endregion 职务管理权限

        #region 部门管理权限

        public const string CreateDepartment = "Sgr.UPMS.CreateDepartment";
        public const string UpdateDepartment = "Sgr.UPMS.UpdateDepartment";
        public const string DeleteDepartment = "Sgr.UPMS.DeleteDepartment";
        public const string ViewDepartment = "Sgr.UPMS.ViewDepartment";

        #endregion 部门管理权限

        #region 菜单管理权限

        public const string CreateMenu = "Sgr.UPMS.CreateMenu";
        public const string UpdateMenu = "Sgr.UPMS.UpdateMenu";
        public const string DeleteMenu = "Sgr.UPMS.DeleteMenu";
        public const string ViewMenu = "Sgr.UPMS.ViewMenu";

        #endregion 菜单管理权限

        #region 日志管理权限

        public const string ViewLogLogin = "Sgr.UPMS.ViewLogLogin";

        #endregion 日志管理权限

        /// <summary>
        /// 权限提供者名称
        /// </summary>
        public string ProviderName => "91-系统管理";

        /// <summary>
        /// 获取所有功能权限
        /// </summary>
        public Task<IEnumerable<FunctionPermission>> GetFunctionPermissionsAsync()
        {
            var permissions = new FunctionPermission[]
            {
                // 组织机构管理权限
                new(CreateOrg, "组织机构", "创建组织", GrantRoleType.SuperAdmin, true),
                new(UpdateOrg, "组织机构", "修改组织", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin),
                new(CancellationOrg, "组织机构", "注销组织", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin),
                new(RecoverOrg, "组织机构", "恢复组织", GrantRoleType.SuperAdmin),
                new(ViewOrg, "组织机构", "浏览组织", GrantRoleType.SuperAdmin),
                new(ViewCurrentOrg, "组织机构", "仅浏览本组织",  GrantRoleType.SystemAdmin),
                //new(AuthenticationOrg, "组织机构", "组织机构认证", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin),
                new(AssociatedParentOrg, "组织机构", "绑定上级组织", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin),

                // 组织邀请管理权限
                new(ManageOrgInvitation, "组织机构", "创建邀请码", GrantRoleType.SystemAdmin),
                new(ViewOrgInvitation, "组织机构", "浏览邀请码", GrantRoleType.SystemAdmin),

                // 账户管理权限
                new(CreateUser, "账户管理", "创建账号", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin),
                new(UpdateUser, "账户管理", "修改账号", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin),
                new(DeleteUser, "账户管理", "删除账号", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin),
                new(ViewUser, "账户管理", "浏览账号", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin),
                new(ModifyUserStatus, "账户管理", "调整状态", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin),
                new(ResetPassword, "账户管理", "重置密码", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin),

                // 个人中心权限
                new(ModifyUser, "个人中心", "修改信息", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin | GrantRoleType.NormalUser),
                new(ModifyPassword, "个人中心", "修改密码", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin | GrantRoleType.NormalUser),

                // 角色管理权限
                new(AllocateFunctionPermissionCommandHandle.RoleCreate, "角色管理", "创建角色", GrantRoleType.SuperAdmin),
                new(AllocateFunctionPermissionCommandHandle.RoleUpdate, "角色管理", "修改角色", GrantRoleType.SuperAdmin),
                new(AllocateFunctionPermissionCommandHandle.RoleDelete, "角色管理", "删除角色", GrantRoleType.SuperAdmin),
                new(AllocateFunctionPermissionCommandHandle.RoleView, "角色管理", "浏览角色", GrantRoleType.SuperAdmin),
                new(AllocateFunctionPermissionCommandHandle.RoleModifyStatus, "角色管理", "调整状态", GrantRoleType.SuperAdmin),
                new(AllocateFunctionPermissionCommandHandle.RoleAllocateFunction, "角色管理", "授予功能权限", GrantRoleType.SuperAdmin),

                // 职务管理权限
                new(CreateDuty, "职务管理", "创建职务", GrantRoleType.SystemAdmin),
                new(UpdateDuty, "职务管理", "修改职务", GrantRoleType.SystemAdmin),
                new(DeleteDuty, "职务管理", "删除职务", GrantRoleType.SystemAdmin),
                new(ViewDuty, "职务管理", "浏览职务", GrantRoleType.SystemAdmin),
                new(ModifyDutyStatus, "职务管理", "调整状态", GrantRoleType.SystemAdmin),

                // 部门管理权限
                new(CreateDepartment, "部门管理", "创建部门", GrantRoleType.SystemAdmin),
                new(UpdateDepartment, "部门管理", "修改部门", GrantRoleType.SystemAdmin),
                new(DeleteDepartment, "部门管理", "删除部门", GrantRoleType.SystemAdmin),
                new(ViewDepartment, "部门管理", "浏览部门", GrantRoleType.SystemAdmin),

                // 菜单管理权限
                new(CreateMenu, "菜单管理", "创建菜单", GrantRoleType.SuperAdmin),
                new(UpdateMenu, "菜单管理", "修改菜单", GrantRoleType.SuperAdmin),
                new(DeleteMenu, "菜单管理", "删除菜单", GrantRoleType.SuperAdmin),
                new(ViewMenu, "菜单管理", "浏览菜单", GrantRoleType.SuperAdmin),

                // 日志管理权限
                new(ViewLogLogin, "登录日志", "浏览登录日志", GrantRoleType.SuperAdmin | GrantRoleType.SystemAdmin )
            };

            return Task.FromResult(permissions.AsEnumerable());
        }
    }
}